﻿using System.Collections.Generic;
using System.Diagnostics;
using Assets.Scripts.Net;
using Assets.Scripts.Net.Data;
using Assets.Scripts.Net.Data.Model;
using Assets.Scripts.Net.Loader;
using Assets.Scripts.UI.Panel;
using UnityEngine;
using UnityEngine.UI;
using System;

namespace Assets.Scripts.UI.Item
{
    /// <summary>
    /// 作业完成的Item
    /// </summary>
    public class HWFinishItem:UIBase<HWFinishItem>
    {
        public Text SeqNumText;
        public Text TitleText;
        public Text ScoreText;
        public Text CorrectText;//批改
        public Button HWFinishButton;

        private HomeWorkInfo info { get; set; }
        protected override UIType UIType
        {
            get { return UIType.Item;}
        }

        protected override void InitOnAwake()
        {
            AutoFindComponent(transform,true);
            HWFinishButton.onClick.AddListener(OnHWFinishButtonClick);
            ExamPanel.OnUpdateScore += UpdateScore;
        }

        public void Init(HomeWorkInfo hwInfo, int finishNum)
        {
            info = hwInfo;
            TitleText.text = hwInfo.Name;
            ScoreText.text = "得分："+hwInfo.Score;
            CorrectText.text = hwInfo.IsCorrect == 0 ? "未批改" : "已批改";
            HWFinishButton.interactable = hwInfo.IsCorrect == 0;
            HWFinishButton.GetComponent<Image>().color = hwInfo.IsCorrect == 1
                ? new Color(1, 1, 1, 0.5f)
                : Color.white;
            DateTime homeworkTime = Convert.ToDateTime(hwInfo.Time);
            //添加对作业截至时间的判断
            if (homeworkTime.CompareTo(DateTime.Now) < 0)
            {
                HWFinishButton.image.color = new Color(1, 1, 1, 0.5f);
                HWFinishButton.interactable = false;
            }
        }

        /// <summary>
        /// 刷新分值
        /// </summary>
        public void UpdateScore(ExamType examType,float score,int id)
        {
            if (examType == ExamType.HomeWork && id == info.Id)
            {
                info.Score = score;
                ScoreText.text ="得分："+ score.ToString();
            }
        }
      

        protected override void InitOnDestroy()
        {
            ExamPanel.OnUpdateScore -= UpdateScore;
        }

        public void Init(HomeWorkInfo hwInfo)
        {
            Init(hwInfo,0);            
        }

        private void OnHWFinishButtonClick()
        {
            List<QueInfo> queInfos =
                NetReaderFactory.Instance().GetInfoByName(NetConstValue.HW_QueInfo + "_" + info.Id) as List<QueInfo>;
            List<AnsInfo> ansInfos =
                NetReaderFactory.Instance().GetInfoByName(NetConstValue.HW_AnsInfo + "_" + info.Id) as List<AnsInfo>;
            if (queInfos != null && ansInfos != null)//说明本地已经保存数据
            {
                ExamPanel panel = CreatePanel(PanelType.ExamPanel).GetComponent<ExamPanel>();
                panel.Init(ExamType.HomeWork, info.Name, 0, queInfos, ansInfos, info.Id);
                return;
            }
            Transform tsf= CreatePanel(PanelType.LoadingLoopPanel);
            tsf.GetComponent<LoadingLoopPanel>().Init();
            Dictionary<string, object> dict = NetManager.Instance.GetDict();
            dict.Add("type", 0);
            dict.Add("paperid", info.Id);
            NetManager.Instance.SendRequest(RequestType.QuestionList, dict, () =>
            {
                if (NetReaderFactory.Instance().NetResUrls.Count > 0)//有需要加载的资源
                {
                    Destroy(tsf.gameObject);
                    LoadingPanel loadingPanel = CreatePanel(PanelType.LoadingPanel).GetComponent<LoadingPanel>();
                    loadingPanel.Init(LoadingType.WWWLoading);
                    Global.Global.Instance.StartCoroutine(WWWLoadManager.Instance()
                        .DownLoadSaveResource(NetReaderFactory.Instance().NetResUrls, () =>
                        {
                            Exit("LoadingPanel(Clone)");
                            List<QueInfo> questions = NetReaderFactory.Instance().GetInfoByType<List<QueInfo>>(NetConstValue.HW_QueInfo + "_" + info.Id) as List<QueInfo>;
                            List<AnsInfo> answers =
                                NetReaderFactory.Instance().GetInfoByType<List<AnsInfo>>(NetConstValue.HW_AnsInfo + "_" + info.Id)
                                    as List<AnsInfo>;
                            ExamPanel panel = CreatePanel(PanelType.ExamPanel).GetComponent<ExamPanel>();
                            panel.Init(ExamType.HomeWork, info.Name, 0, questions, answers, info.Id, OnExamSubmitPaper);
                        }));                   
                }
                else
                {
                    Destroy(tsf.gameObject);
                    List<QueInfo> questions = NetReaderFactory.Instance().GetInfoByType<List<QueInfo>>(NetConstValue.HW_QueInfo + "_" + info.Id) as List<QueInfo>;
                    List<AnsInfo> answers =
                        NetReaderFactory.Instance().GetInfoByType<List<AnsInfo>>(NetConstValue.HW_AnsInfo + "_" + info.Id)
                            as List<AnsInfo>;
                    ExamPanel panel = CreatePanel(PanelType.ExamPanel).GetComponent<ExamPanel>();
                    panel.Init(ExamType.HomeWork, info.Name, 0, questions, answers, info.Id, OnExamSubmitPaper);
                }                   
            });      
        }

        /// <summary>
        /// 提交试卷的回调函数
        /// </summary>
        private void OnExamSubmitPaper(float score)
        {
            info.Score = score;
            info.State = 1;
            Init(info);
        }
    }
}